package com.example.diagnodroid.localisation;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
import android.provider.Settings;
import android.util.Log;
import android.widget.TextView;

import com.example.diagnodroid.R;
import com.google.android.gms.maps.CameraUpdateFactory;
import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.MapFragment;
import com.google.android.gms.maps.model.LatLng;

public class GPSActivity extends Activity {

	private GoogleMap googleMap;
	private LocationManager mLocationManager;
	private MyLocationListener networkLocationListener;
	private MyLocationListener gpsLocationListener;
	private TextView txtLatitude;
	private TextView txtLongitude;
	private TextView txtAltitude;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);

		LocationManager locationManager = (LocationManager) getSystemService(LOCATION_SERVICE);
		boolean isGPSEnabled = locationManager
				.isProviderEnabled(LocationManager.GPS_PROVIDER);
		boolean isNetworkEnabled = locationManager
				.isProviderEnabled(LocationManager.NETWORK_PROVIDER);

		if (!isGPSEnabled && !isNetworkEnabled) {
			showSettingsAlert();
		}

		setContentView(R.layout.gps_layout);

		googleMap = ((MapFragment) getFragmentManager().findFragmentById(
				R.id.map)).getMap();
		txtLatitude = (TextView) findViewById(R.id.resLati);
		txtLongitude = (TextView) findViewById(R.id.resLongi);
		txtAltitude = (TextView) findViewById(R.id.resAlti);

		mLocationManager = (LocationManager) this
				.getSystemService(Context.LOCATION_SERVICE);

		networkLocationListener = new MyLocationListener();
		gpsLocationListener = new MyLocationListener();

		googleMap.setMyLocationEnabled(true);

		Location tmpLocation = mLocationManager
				.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
		if (null == tmpLocation) {
			tmpLocation = mLocationManager
					.getLastKnownLocation(LocationManager.GPS_PROVIDER);
		}
		if (null != tmpLocation) {
			Log.v("LocationActivity",
					"Positionnement sur derniere localisation connue par "
							+ tmpLocation.getProvider() + " : "
							+ tmpLocation.getLatitude() + " - "
							+ tmpLocation.getLongitude());
			googleMap.animateCamera(CameraUpdateFactory.newLatLngZoom(
					new LatLng(tmpLocation.getLatitude(), tmpLocation
							.getLongitude()), 10f));
		}
	}

	@Override
	protected void onResume() {
		// TODO Auto-generated method stub
		super.onResume();

		// Enregistrer ce listener pour recevoir les updates
		mLocationManager
				.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 0, 0,
						networkLocationListener);
		mLocationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER,
				0, 0, gpsLocationListener);

	}

	// arret des updates
	@Override
	protected void onPause() {
		super.onPause();
		mLocationManager.removeUpdates(networkLocationListener);
		mLocationManager.removeUpdates(gpsLocationListener);
	}

	public void showSettingsAlert() {
		AlertDialog.Builder alertDialog = new AlertDialog.Builder(this);

		// Setting Dialog Title
		alertDialog.setTitle("GPS is settings");

		// Setting Dialog Message
		alertDialog
				.setMessage("GPS is not enabled. Do you want to go to settings menu?");

		// On pressing Settings button
		alertDialog.setPositiveButton("Settings",
				new DialogInterface.OnClickListener() {
					public void onClick(DialogInterface dialog, int which) {
						Intent intent = new Intent(
								Settings.ACTION_LOCATION_SOURCE_SETTINGS);
						startActivity(intent);
					}
				});

		// on pressing cancel button
		alertDialog.setNegativeButton("Cancel",
				new DialogInterface.OnClickListener() {
					public void onClick(DialogInterface dialog, int which) {
						dialog.cancel();
					}
				});

		// Showing Alert Message
		alertDialog.show();
	}

	// myListener
	private class MyLocationListener implements LocationListener {

		@Override
		public void onLocationChanged(Location location) {

			// Toast.makeText(getApplicationContext(),
			// " Lat :" + location.getLatitude() + "\n Long : " +
			// location.getLongitude() + "\n Alt : " + location.getAltitude(),
			// Toast.LENGTH_LONG).show();
			txtLatitude.setText("" + location.getLatitude());
			txtLongitude.setText("" + location.getLongitude());
			txtAltitude.setText("" + location.getAltitude());

			googleMap.animateCamera(CameraUpdateFactory.newLatLng(new LatLng(
					location.getLatitude(), location.getLongitude())));

		}

		@Override
		public void onProviderDisabled(String provider) {
		}

		@Override
		public void onProviderEnabled(String provider) {
		}

		@Override
		public void onStatusChanged(String provider, int status, Bundle extras) {
		}

	}
}
